<template>
    <a-card hoverable>
        <a-form ref="formRef" :model="form" :rules="rules" :labelCol="{ span: 6 }">
            <a-row>
                <a-col :span="12">
                    <a-form-item label="最小长度" name="minLength">
                        <a-input-number style="width: 100%" v-model:value="form.minLength" />
                    </a-form-item>
                    <a-form-item label="包含小写字母" name="lowerCase">
                        <a-input-number style="width: 100%" v-model:value="form.lowerCase" />
                    </a-form-item>
                    <a-form-item label="包含数字" name="digits">
                        <a-input-number style="width: 100%" v-model:value="form.digits" />
                    </a-form-item>
                    <a-form-item label="登陆尝试" name="attempts">
                        <a-input-number style="width: 100%" v-model:value="form.attempts" />
                    </a-form-item>

                    <a-form-item label="字符重复" name="occurances">
                        <a-input-number style="width: 100%" v-model:value="form.occurances" />
                    </a-form-item>

                    <a-form-item label="历史密码" name="history">
                        <a-input-number style="width: 100%" v-model:value="form.history" />
                    </a-form-item>
                    <a-form-item label="简单密码" name="dictionary">
                        <a-switch v-model:checked="form.dictionary">
                            <template #checkedChildren><check-outlined /></template>
                            <template #unCheckedChildren><close-outlined /></template>
                        </a-switch>
                    </a-form-item>
                    <a-form-item label="数字序列" name="numerical">
                        <a-switch v-model:checked="form.numerical">
                            <template #checkedChildren><check-outlined /></template>
                            <template #unCheckedChildren><close-outlined /></template>
                        </a-switch>
                    </a-form-item>
                    <!-- /numerical -->
                </a-col>
                <a-col :span="12">
                    <a-form-item label="最大长度" name="maxLength">
                        <a-input-number style="width: 100%" v-model:value="form.maxLength" />
                    </a-form-item>
                    <a-form-item label="包含大写字母" name="upperCase">
                        <a-input-number style="width: 100%" v-model:value="form.upperCase" />
                    </a-form-item>
                    <a-form-item label="特殊字符" name="specialChar">
                        <a-input-number style="width: 100%" v-model:value="form.specialChar" />
                    </a-form-item>
                    <a-form-item label="自动解除" name="duration">
                        <a-input-number style="width: 100%" v-model:value="form.duration">
                            <template #addonAfter>
                                分钟
                            </template>
                        </a-input-number>
                    </a-form-item>
                    <a-form-item label="过期时间" name="expiration">
                        <a-input-number style="width: 100%" v-model:value="form.expiration">
                            <template #addonAfter>
                                天
                            </template>
                        </a-input-number>
                    </a-form-item>
                    <!-- /duration -->
                    <a-form-item label="包含用户名" name="username">
                        <a-switch v-model:checked="form.username">
                            <template #checkedChildren><check-outlined /></template>
                            <template #unCheckedChildren><close-outlined /></template>
                        </a-switch>
                    </a-form-item>
                    <a-form-item label="字母序列策略" name="alphabetical">
                        <a-switch v-model:checked="form.alphabetical">
                            <template #checkedChildren><check-outlined /></template>
                            <template #unCheckedChildren><close-outlined /></template>
                        </a-switch>
                    </a-form-item>
                    <a-form-item label="键盘策略" name="qwerty">
                        <a-switch v-model:checked="form.qwerty">
                            <template #checkedChildren><check-outlined /></template>
                            <template #unCheckedChildren><close-outlined /></template>
                        </a-switch>
                    </a-form-item>

                </a-col>
            </a-row>
        </a-form>
        <div style="text-align: center;">
            <a-button type="primary" @click="submit">提交</a-button>

        </div>
    </a-card>
</template>

<script setup>
import { onMounted, reactive, ref, watch, createVNode } from 'vue';
import { passwordPolicyApi } from '/src/api/security/passwordPolicy/api';
import { Modal, message } from 'ant-design-vue';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import _ from 'lodash';
import { SmartLoading } from '/src/components/framework/smart-loading';
import { smartSentry } from '/src/lib/smart-sentry';


// ----------------------- 表单 ---------------------

const formRef = ref();


const formDefault = {
    id: undefined,
    minLength: undefined,
    lowerCase: undefined,
    digits: undefined,
    attempts: undefined,
    occurances: undefined,
    history: 0,
    dictionary: 0,
    numerical: false,
    maxLength: undefined,
    upperCase: undefined,
    specialChar: undefined,
    duration: undefined,
    expiration: undefined,
    username: undefined,
    alphabetical: undefined,
    qwerty: undefined
};

let form = reactive({ ...formDefault });

// 表单规则
const rules = {
    minLength: [{ required: true, message: '不能为空' }],
    lowerCase: [{ required: true, message: '不能为空' }],
    digits: [{ required: true, message: '不能为空' }],
    attempts: [{ required: true, message: '不能为空' }],
    occurances: [{ required: true, message: '不能为空' }],
    history: [{ required: true, message: '不能为空' }],
    dictionary: [{ required: true, message: '不能为空' }],
    numerical: [{ required: true, message: '不能为空' }],
    maxLength: [{ required: true, message: '不能为空' }],
    upperCase: [{ required: true, message: '不能为空' }],
    specialChar: [{ required: true, message: '不能为空' }],
    duration: [{ required: true, message: '不能为空' }],
    expiration: [{ required: true, message: '不能为空' }],
    username: [{ required: true, message: '不能为空' }],
    alphabetical: [{ required: true, message: '不能为空' }],
    qwerty: [{ required: true, message: '不能为空' }],
};



//

// ----------------------- 部门树的展示 ---------------------
async function submit() {

    form.status = form.status ? 1 : 0
    form.username = form.username ? 1 : 0
    form.alphabetical = form.alphabetical ? 1 : 0
    form.qwerty = form.qwerty ? 1 : 0
    form.dictionary = form.dictionary ? 1 : 0
    form.numerical = form.numerical ? 1 : 0
    let res = await passwordPolicyApi.updatepasswordpolicy(form);
    if (res.code == 0) {
        message.success("修改成功")
        init();
    }
}

onMounted(() => {
    init();
});

// 
async function init() {
    try {

        let res = await passwordPolicyApi.getpasswordpolicy();
        if (res.code == 0) {
            form.id = res.data.id;
            form.minLength = res.data.minLength;
            form.lowerCase = res.data.lowerCase;
            form.digits = res.data.digits
            form.attempts = res.data.attempts

            form.occurances = res.data.occurances

            form.history = res.data.history
            form.dictionary = res.data.dictionary == 1 ? true : false
            form.numerical = res.data.numerical == 1 ? true : false

            form.maxLength = res.data.maxLength
            form.upperCase = res.data.upperCase

            form.specialChar = res.data.specialChar


            form.status = form.status == 1 ? true : false
            
            form.duration = res.data.duration
            form.expiration = res.data.expiration
            form.username = res.data.username == 1 ? true : false
            form.alphabetical = res.data.alphabetical == 1 ? true : false
            form.qwerty = res.data.qwerty == 1 ? true : false
        }
    } catch (e) {
        smartSentry.captureError(e);
    } finally {
       
    }
}



// 重置
function resetQuery() {
    queryParam.value.providerName = ""
    queryParam.value.pageSize = 10
    queryParam.value.pageNumber = 1
    queryParam.value.total = 0
    onSearch();
}

// 搜索
function onSearch() {
    init();
}



// ----------------------- 表单操作：添加部门/修改部门/删除部门/上下移动 ---------------------


// 编辑
function updateRecord(e) {
    getRecordById(e);
    title.value = "编辑"
    visible.value = true
}

async function getRecordById(id) {
    let res = await socialsproviderApi.getSocialsProviderById(id);
    disabledCode.value = true
    if (res.code == 0) {
        form.id = res.data.id
        form.icon = res.data.icon
        form.provider = res.data.provider
        form.providerName = res.data.providerName
        form.clientId = res.data.clientId
        form.clientSecret = res.data.clientSecret
        form.agentId = res.data.agentId
        form.scanCode = res.data.scanCode
        form.display = res.data.display
        form.sortIndex = res.data.sortIndex
        form.status = res.data.status

    }

}

// 删除
function deleterRecord(id) {
    Modal.confirm({
        title: '提醒',
        icon: createVNode(ExclamationCircleOutlined),
        content: '确定要删除吗?',
        okText: '删除',
        okType: 'danger',
        async onOk() {
            SmartLoading.show();
            try {
                await socialsproviderApi.deleteSocialsProvider(id);
                init();
            } catch (error) {
                smartSentry.captureError(error);
            } finally {
                SmartLoading.hide();
            }
        },
        cancelText: '取消',
        onCancel() { },
    });
}
</script>

<style scoped lang="less"></style>